


//
//  NCaiwuDataInqDetailVC.m
//  nncloud
//
//  Created by macfai on 2018/12/21.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NCaiwuDataInqDetailVC.h"
#import "NInqueryModel.h"
#import "NDataInqView.h"
#import "NCaiwuInqHeaderView.h"
#import "ITDatePickerController.h"
@interface NCaiwuDataInqDetailVC ()<UITableViewDataSource,UITableViewDelegate,ITDatePickerControllerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,strong)NSArray *titleDetailArray;

@property(nonatomic,strong)NSArray *pickerArray;

@property(nonatomic,strong)UILabel *startLabel;

@property(nonatomic,strong)UILabel *endLabel;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,copy)NSString *startTime;
@property (nonatomic,copy)NSString *endTime;



@end

@implementation NCaiwuDataInqDetailVC

#define cellIdentifier @"datai"
#define SectionHeaderHeight  45.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50.f;
        _tableView.backgroundColor = kBackgroundColor;
        _tableView.separatorColor = [UIColor clearColor];
        ///用系统cell 时 最好在cell 的代理方法中进行设置
//        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        //        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"财务数据查询" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    //    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"搜索" selectedIMG:nil tartget:self action:@selector(search)];
    self.dataArray = [NSMutableArray array];
    self.titleArray = @[@"营业收入(单位:千元)",@"其中:主营业务收入(单位:千元)",
                        @"营业成本(单位:千元)",@"其中:主营业务成本(单位:千元)",
                        @"营业税金及附加(单位:千元)",@"其中:营业税金及附加(单位:千元)",
                        @"销售费用(单位:千元)",@"管理费用(单位:千元)",
                        @"其中:税金(单位:千元)",@"财务费用(单位:千元)",
                        @"其中:利息收入(单位:千元)",@"利息支出(单位:千元)",
                        @"资产减值损失(单位:千元)",@"公允价值变动收益(单位:千元)",
                        @"投资收益(单位:千元)",@"营业利润(单位:千元)",
                        @"营业外收入(单位:千元)",@"其中:政府补助(单位:千元)",
                        @"营业外支出(单位:千元)",@"利润总额(单位:千元)",];
    [self.view addSubview:self.tableView];
    [self addHeader];
    [self inqData];
}

///查询财务数据
- (void)inqData{
    WeakStament(ws);
    NSDictionary *dict;
    if ([[NUserModel shareInstance].userInfoType integerValue]==1) {
        //企业端
        dict = @{@"companyId":[NUserModel shareInstance].userInfoId?:@"5",
                 @"startTime":self.startTime?:@"2018-01",
                 @"endTime":self.endTime?:@"2018-12",
                 };
    }else{
        ///政府端
        dict = @{
                 @"startTime":self.startTime?:@"2018-01",
                 @"endTime":self.endTime?:@"2018-12",
                 };
    }
    
    [[NNRCNetRequest manager]getWithUrl:kCaiwuDataInqUrl params:dict success:^(id result) {
        YYLog(@"%@",result);
        
        if ([result[@"page"] isKindOfClass:[NSNull class]]) {
            [AFToast showText:@"查询数据结果为空"];
            return ;
        }
        NSDictionary *dict = result[@"page"];
        NSArray *temp =  [dict allValues];
        YYLog(@"%@",temp);
        NSMutableArray *mutArray = [NSMutableArray arrayWithArray:temp];
        
        
        ///数组遍历  注意是可变数组这样进行遍历
        //正序
        [mutArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[NSNull class]]) {
                [mutArray removeObject:obj];
                YYLog(@"%@",mutArray);
                ws.dataArray = [mutArray copy];
            }
        }];
        //逆序 数组遍历
//        [mutArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            ///后台返回的null 根据我们这边的NSNull来判断
//            if ([obj isKindOfClass:[NSNull class]]) {
//                [mutArray removeObject:obj];
//                YYLog(@"%@",mutArray);
//                self.dataArray = [mutArray copy];
//            }
//        }];
        [ws.tableView reloadData];
    
    } failure:^(id failure) {
        
    }];
    
}
- (void)fillDataWithModel:(NInqueryModel *)model{
    
//    self.sumLabel.text = model.industryYield;
//    self.sellLabel.text = model.industrySell;
//    self.exportLabel.text = model.industryExport;
}


- (void)addHeader{
    //自定义xib view  需要生成xib 后将这个class  类指向xib 就可了
    NCaiwuInqHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"NCaiwuInqHeaderView" owner:self options:nil] lastObject];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startAction)];
    [header.startLabel addGestureRecognizer:tap];
    self.startLabel = header.startLabel;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endAction)];
    [header.endLabel addGestureRecognizer:tap1];
    self.endLabel = header.endLabel;
    self.tableView.tableHeaderView = header;
}

- (void)startAction{
    
    YYLog(@"开始");
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 620;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
}

- (void)endAction{
    
    YYLog(@"结束");
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 621;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
    
}

#pragma mark - 年月选择器
- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString{
    if (datePickerController.tag==620) {
        YYLog(@"%@",dateString);
        
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        [mut insertString:@"-" atIndex:4];
        self.startTime = (NSString *)mut;
        self.startLabel.text = self.startTime;
        
    }else{
        YYLog(@"%@",dateString);
        
        //        self.endTime = dateString;
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        [mut insertString:@"-" atIndex:4];
        self.endTime = (NSString *)mut;
        self.endLabel.text = self.endTime;
        //点完结束时间的确认后进行数据查询
        [self inqData];
    }
}


#pragma mark - datasource

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//
//    //    return  self.dataArray.count;
//    return 1;
//
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NInqueryModel *model = self.dataArray[indexPath.row];
    //系统自带的cell 使用时需要用这种重用方式
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.selectionStyle = 0;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.textColor = kDarkGrayColor;
    if (self.dataArray.count>0) {
        cell.detailTextLabel.text = self.dataArray[indexPath.row];
    }
    
    return cell;
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    NMyQiyeModel *model = self.dataArray[indexPath.section];
    //    //    NQIyeRecruitDetailVC *detail = [NQIyeRecruitDetailVC new];
    //    //    detail.zhaopinId = model.empInfoId;
    //    //    [self.navigationController pushViewController:detail animated:YES];
    //    NMyQiyeViewController *myQi = [NMyQiyeViewController new];
    //    myQi.eid = model.enterpriseId;
    //    myQi.userType = [NUserModel shareInstance].userInfoType;
    //    [self.navigationController pushViewController:myQi animated:YES];
}

#pragma mark - section head
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25, 0, ScreenWidth-25, SectionHeaderHeight)];
//    label.text = @"2018.05-2018.10";
//    [view addSubview:label];
//    return view;
//}

//
//#pragma mark - section foot
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 0;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    return nil;
//}
//

@end
